﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Text;
using SdmxMl.Structure;
using SdmxMl.Common;

namespace SdmxMl.Msg
{
    public class HeaderType
    {
        public enum ReferenceScheme
        {
            Structure,
            MetadataSet,
            DataSet,

        }

        public enum EnumDataSetAction
        {
            Append,
            Replace,
            Delete,
            Information
        }

        private PartyType _sender = new PartyType();
        private PartyType _receiver = new PartyType();
        private string _id;
        private bool _test;
        private bool _truncated;
        private MultiLang _name;
        private EnumDataSetAction _datasetaction;
        private string _extracted;
        private string _prepared;
        private string _datasetagency;
        private string _datasetid;

        public HeaderType()
        {
            // Default to unique GUID
            Id = Guid.NewGuid().ToString();
            _name = new MultiLang();
        }

        #region Properties

        public PartyType Sender
        {
            get { return _sender; }
            set { _sender = value; }
        }

        public PartyType Receiver
        {
            get { return _receiver; }
            set { _receiver = value; }
        }

        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public bool Test
        {
            get { return _test; }
            set { _test = value; }
        }

        public bool Truncated
        {
            get { return _truncated; }
            set { _truncated = value; }
        }

        public MultiLang MsgName
        {
            get { return _name; }
            set { _name = value; }
        }

        public string Prepared
        {
            get { return _prepared; }
            set { _prepared = value; }
        }

        public string DataSetAgency
        {
            get { return _datasetagency; }
            set { _datasetagency = value; }
        }

        public string DataSetID
        {
            get { return _datasetid; }
            set { _datasetid = value; }
        }

        public EnumDataSetAction DataSetAction
        {
            get { return _datasetaction; }
            set { _datasetaction = value; }
        }

        public string Extracted
        {
            get { return _extracted; }
            set { _extracted = value; }
        }

        #endregion

        private static void WriteReferencesHeader_2_0(XmlTextWriter xw)
        {

            xw.WriteComment("Document Generated by SAE-FMR (www.sdmx.ch) on " + DateTime.Now.ToShortDateString());
            xw.WriteStartElement("Structure");
            xw.WriteAttributeString("xmlns", "http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message");
            xw.WriteAttributeString("xmlns", "common", null, "http://www.SDMX.org/resources/SDMXML/schemas/v2_0/common");
            xw.WriteAttributeString("xmlns", "structure", null, "http://www.SDMX.org/resources/SDMXML/schemas/v2_0/structure");
            xw.WriteAttributeString("xmlns", "utility", null, "http://www.SDMX.org/resources/SDMXML/schemas/v2_0/utility");
            xw.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
            xw.WriteAttributeString("xsi", "schemaLocation", null, "http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message SDMXMessage.xsd");
        }
        private static void WriteReferencesHeader_2_1(XmlTextWriter xw)
        {

            xw.WriteComment("Experimental SDMX 2.1 Document Generated by SAE-FMR (www.sdmx.ch) on " + DateTime.Now.ToShortDateString());
            xw.WriteStartElement("message:Structure");
            xw.WriteAttributeString("xmlns", "message", null, "http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message");
            xw.WriteAttributeString("xmlns", "structure", null, "http://www.sdmx.org/resources/sdmxml/schemas/v2_1/structure");
            xw.WriteAttributeString("xmlns", "common", null, "http://www.sdmx.org/resources/sdmxml/schemas/v2_1/common");
            xw.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
            xw.WriteAttributeString("xsi", "schemaLocation", null, "http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message ./SDMXMessage.xsd");
        }

        /// <summary>SDMX schema references </summary>
        public static void WriteReferencesHeader(XmlTextWriter xw)
        {
            if (SdmxNamespaceManager.SdmxTarget == SdmxVersion.SDMX_21)
                WriteReferencesHeader_2_1(xw);
            else
                WriteReferencesHeader_2_0(xw);
        }

        /// <summary>SDMX schema references </summary>
        public static void WriteReferencesGenMetaHeader(XmlTextWriter xw)
        {
            xw.WriteComment("Document Generated by SFO-SMS on " + DateTime.Now.ToShortDateString());
            xw.WriteStartElement("GenericMetadata");
            xw.WriteAttributeString("xmlns", "http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message");
            xw.WriteAttributeString("xmlns", "genericmetadata", null, "http://www.SDMX.org/resources/SDMXML/schemas/v2_0/genericmetadata");
            xw.WriteAttributeString("xmlns", "common", null, "http://www.SDMX.org/resources/SDMXML/schemas/v2_0/common");
            xw.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
            xw.WriteAttributeString("xsi", "schemaLocation", null, "http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message SDMXMessage.xsd");
        }

        public void WriteXml(XmlTextWriter xw, ReferenceScheme refS)
        {
            if (refS == ReferenceScheme.MetadataSet)
                WriteReferencesGenMetaHeader(xw);
            else
                WriteReferencesHeader(xw);

            if (SdmxNamespaceManager.SdmxTarget == SdmxVersion.SDMX_21)
            {
                xw.WriteStartElement("message", "Header", null);
                xw.WriteElementString("message", "ID", null, _id);
                xw.WriteElementString("message", "Test", null, Test.ToString().ToLower());

                DateTime now = DateTime.Now;
                string dt = now.ToString("yyyy-MM-dd") + "T" + now.ToString("HH:mm:ss-00:00");
                xw.WriteElementString("message", "Prepared", null, dt);

                _sender.WriteXml(xw, "message", "Sender");

                MsgName.WriteXml(xw, "common", "Name");

            }
            else // SDMX 2.0
            {
                xw.WriteStartElement("Header");

                xw.WriteElementString("ID", _id);
                xw.WriteElementString("Test", Test.ToString().ToLower());

                MsgName.WriteXml(xw, null, "Name");

                DateTime now = DateTime.Now;
                string dt = now.ToString("yyyy-MM-ddTHH:mm:ss");
                xw.WriteElementString("Prepared", dt);

                _sender.WriteXml(xw, null, "Sender");
            }


            xw.WriteEndElement();

            // SDMX 2.1 Start Structure
            if (SdmxNamespaceManager.SdmxTarget == SdmxVersion.SDMX_21)
                xw.WriteStartElement("message", "Structures", null);

        }


    }
}

